package com.platform.user.center.portal.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.platform.user.center.portal.entity.AlipayUser;
import com.platform.user.center.portal.entity.VirtualTransitCard;
import com.platform.user.center.portal.service.AlipayUserService;
import com.platform.user.center.portal.service.VirtualTransitCardService;
import com.platform.user.center.portal.util.BaseResponse;
import com.platform.user.center.portal.util.HttpUtils;
import com.platform.user.center.portal.util.PageUtils;
import com.platform.user.center.portal.util.ResultCodeAndMessage;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.Condition;

/**
 * Created by churcy on 2017/6/9.
 */
@RestController
public class AlipayVirtualCardController {

    @Autowired
    private VirtualTransitCardService virtualTransitCardService;

    @Autowired
    private AlipayUserService alipayUserService;

    @GetMapping(value = "/card/list")
    public Object cardList(HttpServletRequest request) {
        Map<String, Object> response = new HashMap<>();
        Map<String, Object> result = new HashMap<>();
        try {
            do {

                String cardStatus = request.getParameter("cardStatus");
                String cardNo = request.getParameter("cardNo");
                String userId = request.getParameter("userId");
                String cardType = request.getParameter("cardType");
                String checkEndTime = request.getParameter("checkEndTime");
                String pageSize = request.getParameter("pageSize");
                String pageNum = request.getParameter("pageNum");
                String createTime = request.getParameter("createTime");
                String mobilePhone = request.getParameter("mobilePhone");
                String userName = request.getParameter("userName");


                Map<String, Object> conditions = new HashMap<>();

                conditions.put("cardStatus", cardStatus);
                conditions.put("cardNo", cardNo);
                conditions.put("userId", userId);
                conditions.put("cardType", cardType);
                conditions.put("checkEndTime", checkEndTime);
                conditions.put("createTime", createTime);
                conditions.put("pageSize", pageSize != null ? pageSize : "10");
                conditions.put("pageNum", pageNum != null ? pageNum : "1");
                conditions.put("userName", userName != null ? URLDecoder.decode(userName, "UTF-8") : null);
                conditions.put("mobilePhone", mobilePhone);

                List<Map<String, String>> virtualTransitCardList = virtualTransitCardService.findByConditions(conditions);

                result.put("virtualTransitCardList", virtualTransitCardList);
                result.put("page", PageUtils.formatPage(new PageInfo(virtualTransitCardList)));

                response = BaseResponse.setResponse(result, ResultCodeAndMessage.SUCCESS);
            } while (false);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return response;
    }

    @PostMapping(value = "/card/revoked")
    public Object revokedCard(HttpServletRequest request) {
        String url = "https://zf.wuhanbus.com:8443/api/virtual/card/local/revoked";
        String appId = request.getParameter("appId");
        String cardType = request.getParameter("cardType");
        Map<String, Object> response = new HashMap<>();
        Map<String, Object> result = new HashMap<>();
        try {
            do {
                String userId = request.getParameter("userId");
                String mobilePhone = request.getParameter("mobilePhone");
                if (StringUtils.isNotBlank(mobilePhone)) {

                }
                StringBuffer sb = new StringBuffer();
                sb.append("userId=" + userId);
                sb.append("&action=" + "revoke");
                sb.append("&disabledTips=" + "系统统一退卡");
                sb.append("&appId=" + appId);
                sb.append("&cardType=" + cardType);
                sb.append("&sign=" + "RSA2");
                String apiResult = HttpUtils.httpPost(url, null, sb.toString());
                result.put("result", apiResult);
                response = BaseResponse.setResponse(result, ResultCodeAndMessage.SUCCESS);
            } while (false);
        } catch (IOException e) {
            response = BaseResponse.setResponse(null, ResultCodeAndMessage.SYS_ERROR);
            e.printStackTrace();
        }

        return response;
    }

    @PostMapping(value = "/disabled/card/revoked")
    public Object revokedAllDisabledCard(HttpServletRequest request) {
        try {
            String url = "https://zf.wuhanbus.com:8443/api/virtual/card/local/revoked";

            String checkEndTime = request.getParameter("checkEndTime");
            String pageSize = request.getParameter("pageSize");
            String pageNum = request.getParameter("pageNum");
            String createTime = request.getParameter("createTime");
            String appId = request.getParameter("appId");
            String cardType = request.getParameter("cardType");
            Map<String, Object> conditions = new HashMap<>();
            conditions.put("cardStatus", "disabled");
            conditions.put("checkEndTime", checkEndTime);
            conditions.put("pageSize", pageSize != null ? pageSize : "10");
            conditions.put("pageNum", pageNum != null ? pageNum : "1");

            List<Map<String, String>> virtualTransitCardList = virtualTransitCardService.findByConditions(conditions);

            for (Map<String, String> virtualTransitCard : virtualTransitCardList) {
                StringBuffer sb = new StringBuffer();
                sb.append("userId=" + virtualTransitCard.get("userId"));
                sb.append("&action=" + "revoke");
                sb.append("&disabledTips=" + "系统统一退卡");
                sb.append("&appId=" + appId);
                sb.append("&cardType=" + cardType);
                sb.append("&sign=" + "RSA2");
                String apiResult = HttpUtils.httpPost(url, null, sb.toString());
                System.out.println(apiResult);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @PostMapping(value = "/all/disabled/card/revoked")
    public Object revokedAll(HttpServletRequest request) {
        try {
            String url = null;
            String appId = request.getParameter("appId");
            String cardType = request.getParameter("cardType");
            Map<String, Object> conditions = new HashMap<>();
            conditions.put("cardStatus", "disabled");
            conditions.put("pageSize", "1000");
            conditions.put("pageNum", "1");
            String sign = null;
            List<Map<String, String>> virtualTransitCardList = new ArrayList<>();
            switch (appId) {
                case "2017022705927164"://武汉
                    sign = "RSA2";
                    url = "https://zf.wuhanbus.com:8443/api/virtual/card/local/revoked";
                    virtualTransitCardList = virtualTransitCardService.findByConditions(conditions);
                    break;
                case "2017071807800853"://黄岛
                    sign = "RSA2";
                    url = "https://qd.zhenqingbus.com:8443/api/virtual/card/local/revoked";
                    virtualTransitCardList = virtualTransitCardService.findDisabled();
                    break;
                case "2017081608219705"://嘉兴
                    sign = "RSA";
                    url = "https://jxgjzfb.jiaxingbus.com:8443/api/virtual/card/local/revoked";
                    virtualTransitCardList = virtualTransitCardService.findDisabled();
                    break;
                case "2017091408726612"://嘉兴
                    sign = "RSA";
                    url = "https://sjzgj.i-xiaoma.com.cn:8443/api/virtual/card/local/revoked";
                    virtualTransitCardList = virtualTransitCardService.findDisabled();
                    break;
                case "2017080908104940"://泰安
                    sign = "RSA";
                    url = "https://ic.tabus.com.cn:8443/api/virtual/card/local/revoked";
                    virtualTransitCardList = virtualTransitCardService.findDisabled();
                    break;
//                default:
//                    sign = "RSA";
//                    url = "https://xmbus.i-xiaoma.com.cn:9191/api/virtual/card/alipay/status";
//                    virtualTransitCardList =
            }


            for (Map<String, String> virtualTransitCard : virtualTransitCardList) {
                StringBuffer sb = new StringBuffer();
                sb.append("userId=" + virtualTransitCard.get("userId"));
                sb.append("&action=" + "revoke");
                sb.append("&disabledTips=" + "系统统一退卡");
                sb.append("&appId=" + appId);
                sb.append("&cardType=" + cardType);
                sb.append("&sign=" + sign);
                String apiResult = HttpUtils.httpPost(url, null, sb.toString());
                System.out.println(apiResult);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @GetMapping(value = "/all/disabled/card/revoked/error")
    public Object revokeErrorUid(HttpServletRequest request) {
        try {
            String url = "https://zf.wuhanbus.com:8443/api/virtual/card/local/revoked";
            String appId = request.getParameter("appId");
            String cardType = request.getParameter("cardType");
            File file = new File("/Users/churcy/Desktop/error_user_id.json");
            if (!file.exists()) {
                System.out.println("file not exist");
            }

            FileReader fileReader = new FileReader(file);
            InputStreamReader read = new InputStreamReader(
                    new FileInputStream(file));//考虑到编
            BufferedReader bufferedReader = new BufferedReader(read);
            String lineTxt = null;
            String finalStr = null;
            while ((lineTxt = bufferedReader.readLine()) != null) {
                finalStr += lineTxt;
            }
            fileReader.close();
            read.close();
            finalStr = finalStr.replace("null", "");
            System.out.println(finalStr);
            JSONObject jsonObject = JSONObject.parseObject(finalStr);
            JSONArray jsonArray = jsonObject.getJSONArray("RECORDS");
            for (Object temp : jsonArray) {
                Map tempMap = (Map) temp;
                String userId = (String) tempMap.get("user_id");
                VirtualTransitCard virtualTransitCard = virtualTransitCardService.getByUserId(userId);
                if (virtualTransitCard != null) {
                    virtualTransitCard.setDeleted("FALSE");
                    virtualTransitCardService.update(virtualTransitCard);
                }
                StringBuffer sb = new StringBuffer();
                sb.append("userId=" + userId);
                sb.append("&action=" + "revoke");
                sb.append("&disabledTips=" + "系统统一退卡");
                sb.append("&appId=" + appId);
                sb.append("&cardType=" + cardType);
                sb.append("&sign=" + "RSA2");
                String apiResult = HttpUtils.httpPost(url, null, sb.toString());
                System.out.println(apiResult);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
